package org.jiangnantaste.facade;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.jiangnantaste.domain.Image;
import org.jiangnantaste.service.ImageService;
import org.jiangnantaste.serviceimpl.ImageServiceImpl;
import org.jiangnantaste.utils.ImageHelper;
import org.jiangnantaste.vo.ImageVo;


public class ListAllImagesFacade extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ListAllImagesFacade() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
	
	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			PrintWriter out = response.getWriter();
			
			ImageService  service = new ImageServiceImpl();
			List<Image> list = service.listAllImage();
			List<ImageVo> vlist = new ArrayList<ImageVo>();
			for (Image im : list){
				ImageVo vim = new ImageVo();
				vim.setImageId(im.getImageId());
				vim.setImageName(im.getImageName());
				vim.setPathName(im.getPathName());
				vim.setActive(im.isActive());
				vim.setImageData(ImageHelper.encode(im.getImageData()));
				
				vlist.add(vim);
			}
			JSONArray jarr = JSONArray.fromObject(vlist);
			out.print(jarr.toString());
		} catch (Exception e){
			e.printStackTrace();
		}
		 
	}

}
